<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	if(!$_RQ['code']){
		exi('请重新扫码！','error');
	}
	$code = trim($_RQ['code']);
	if(!$this->check_seri_num($code)){
		exi('挪车码无效！','error');
	}
	$return['basic']=$this->plugin_setting('basic');
	$return=array();
	//根据序列号查询挪车卡
	$card=pdo_get('jy_move_card',array('code'=>$code,'uniacid'=>$_SESSION['uniacid']),array('id','mid','status'));
	if(!$card){
		cfc('core')->checklogin();
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'code'=>$code,
			'status'=>1,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_move_card',$data);
		$id=pdo_insertid();
		exi('激活成功！','','jy_move/move_card/edit/id='.$id);
	}
	if($_SESSION['uid'] && !pdo_getcolumn('jy_move_member',array('id'=>$_SESSION['uid']),'id')){
		$new_member=array(
			'id'=>$_SESSION['uid'],
			'uniacid'=>$_SESSION['uniacid'],
			'name'=>$_SESSION['member']['nickname'],
			'status'=>1,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_move_member',$new_member);
	}
	if(!$card['status']){
		$return['message']='挪车卡已失效！';
	}else{
		$return['item']=array();
	}
	//根据挪车卡mid查询车主信息及其设置
	$member=pdo_get('jy_move_member',array('id'=>$card['mid']));
	//车主id存入session
	$this->session('member_id',$member['id']);
	$this->session('card_id',$card['id']);
	$return['member']=$member;
	exi($return);
	
}
if($_RQ['op']=='notice'){
	$post=get_postdata();
	if(!$post['image']){
		exi('请先上传现场照片！','error');
	}
	$member_id=$this->session('member_id');
	$card_id=$this->session('card_id');
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$member_id,
		'card_id'=>$card_id,
		'mover_id'=>$_SESSION['uid'],
		'image'=>$post['image'],
		'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_move_log',$insert);
	$id=pdo_insertid();
	if($_SESSION['uid'] && $post['mobile']){
		pdo_update('jy_move_member',array('mobile'=>$post['mobile']),array('id'=>$_SESSION['uid']));
	}
	//获得车主通知方式设置
	
	$member=pdo_get('jy_move_member',array('id'=>$member_id));
	$notice_type=explode(";",$member['notice_type']);
	if(in_array('1',$notice_type)){
		$postdata = array(
					'first' => array(
						'value' => '您好，你有一条挪车提醒',
						'color' => '#990000'
					),
					'keyword1' => array(
						'value' => '系统通知',
						'color' => '#ff510'
					),
					'keyword2' => array(
						'value' => '您的爱车挡住路啦，麻烦您给挪一下呗',
						'color' => '#ff510'
					),
					'remark' => array(
						'value' => '请尽快去挪一下您的爱车，谢谢',
						'color' => '#ff510'
					),							
				);
		$tpl=$this->plugin_setting('message_tpl');
		$url=custom_url('jy_move/log/detail/id='.$id,true);
		cfc('account')->sendTplNotice($member_id,$tpl['notice'],$postdata,$url,'#980000');
	}
	//短信通知
	if(in_array('2',$notice_type) && $member['mobile']){
		$setting=$this->plugin_setting('sms_tpl');
		$card_title=pdo_getcolumn('jy_move_card',array('id'=>$card_id),'title');
		$card_title=$card_title?$card_title:'未设置';
		$this->plugin('jy_alisms')->sendsms($member['mobile'],array('card_title'=>$card_title),$setting['move']);
	}
	//语音短信
	if(in_array('3',$notice_type) && $member['mobile']){
		$setting=$this->plugin_setting('say_tpl');
		$card_title=pdo_getcolumn('jy_move_card',array('id'=>$card_id),'title');
		$card_title=$card_title?$card_title:'未设置';
		$this->plugin('jy_alisms')->sendsms($member['mobile'],array('card_title'=>$card_title),$setting['move']);
	}
	//隐私通话
	if(in_array('4',$notice_type) && $post['mobile'] && $member['mobile']){
		$bind_mobile=$this->plugin('jy_alisms')->bind($post['mobile'],$member['mobile']);
		if(is_error($bind_mobile)){
			exi('已成功通知车主，创建隐私通话专线失败，请联系管理员！','error');
		}
		exi('已成功通知车主，并创建隐私通话专线，点击确定联系车主！','','','tel:'.$bind_mobile);
	}
	exi('已成功通知车主，请耐心等待车主回复！');
}

?>